<?php
// 订单管理
namespace NewAdmin\Controller;
use Think\Controller;
class MallController extends CommonController {
    public function index(){
    	
  		$order=M('order');
  		if($_GET['order_status']!=null){
  			$status=I('get.order_status');
  			
  			if($status=='10'){$where['order_state']='10';$where['refund_state']='0';}
  			if($status=='20'){$where['order_state']='20';$where['refund_state']='0';}
  			if($status=='30'){$where['order_state']='30';$where['refund_state']='0';}
  			if($status=='40'){$where['order_state']='40';$where['refund_state']='0';}
  			if($status=='0'){$where['order_state']='0';$where['refund_state']='0';}
  			if($status=='-1'){$where['refund_state']=array('between','1,3');}
  			if($status=='-2'){$where['refund_state']='4';}
  		}
  		
  		if($_GET['startDate']!=null){
  			$startDate=strtotime(I('get.startDate'));
  		}else{
  			$startDate='0';
  		}
  		
  		if($_GET['endDate']!=null){
  			$endDate=strtotime(I('get.endDate'));
  		}else{
  			$endDate='0';
  		}
  		if($startDate>0||$endDate>0){
  			$where['add_time']=array('between',array($startDate,$endDate));
  		}
  		
  		if($_GET['oreder_name']!=null){
  			$where['order_sn']=$_GET['oreder_name'];
  		}
  		
  		
  		
    	//全部
    	$count=$order->where($where)->count();
    	$Page = new \Think\Page ($count,16); // 实例化分页类 传入总记录数
		$Page->setConfig('header','条记录');
	    $Page->setConfig('prev','上一页');
	    $Page->setConfig('next','下一页');
		$show = $Page->show(); // 分页显示输出
		$order_list=$order->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('order_id desc')->select();
    	$this->assign('order_list',$order_list);
    	$this->assign('page',$show);
    	$this->assign('count',$count);
    
    	$this->assign('title','订单管理');
    	$this->display();
    }
    
    function orderdetail(){
  		
    	$order_id=I('get.id');
    	$order=M('order');
    	$order_goods=M('order_goods');
    	$order_info=$order->where('order_id='.$order_id)->find();
    	$order_goods_info=$order_goods->where('order_id='.$order_id)->select();
      $order_price_action=M('order_price_action')->where('order_id='.$order_id)->order('action_id desc')->find();
    	$this->assign('order_price_action',$order_price_action);
    	$this->assign('order_goods_info',$order_goods_info);
    	$this->assign('order_info',$order_info);
    	$this->assign('title','订单详情');
    	$this->display();
  }
  
   function get_orderlogistics(){
  	if($_POST){
  		$order_id=I('post.order_id');
  		$order=M('order');
  		$order_info=$order->find($order_id);
  		$result=getOrderTracesByJson($order_info['shipping_company_id'],$order_info['shipping_code'],'1304146','f0e280a8-d7b0-470a-b1bd-8d4a9584a78e','http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx');
  		$this->ajaxReturn($result);
  	}
  }
    function get_delivery(){
  	if($_POST){
  		$order_id=I('post.order_id');
  		$express=M('express');
  		$express_array=$express->select();
  		$order=M('order');
  		$order_info=$order->find($order_id);
  		$ProvinceInfo=getProvinceInfo($order_info['buyer_province']);
			$CityInfo=getCityInfo($order_info['buyer_city']);
			$DistrictInfo=getDistrictInfo($order_info['buyer_district']);
			$address='<br/>姓名：'.$order_info['buyer_name'].'<br/>手机：'.$order_info['buyer_mobile'].'<br/>地址：'.$ProvinceInfo['province_name'].$CityInfo['city_name'].$DistrictInfo['district_name'].$order_info['buyer_address'];
			$data['express_company_list']=$express_array;
			$data['order_info']['address']=$address;
			$this->ajaxReturn($data);
			
  	}
  }
  
  function put_delivery(){
  	if($_POST){
  		$order_id=I('post.order_id');
		$data['shipping_name']=I('post.express_name');
		$data['shipping_company_id']=I('post.express_company_id');
		$data['shipping_code']=I('post.express_no');
		$data['order_state']='30';
		$data['shipping_time']=time();
		$order=M('order');
	
		$rs=$order->where("order_id=$order_id")->save($data);
		
		if($rs){
			$log['order_id']=$order_id;
			$log['action']='系统发货';
			$log['user_name']='总后台';
			$log['order_status']='30';
			$log['order_status_text']='总后台发货';
			$log['action_time']=time();
			M('order_action')->add($log);
			$message['code']=1;
			$message['message']='发货成功';
			$order_info=$order->find($order_id);
			$user_info=M('users')->find($order_info['buyer_id']);
			
			if($user_info['wx_h5openid']){
				
				$openid=$user_info['wx_h5openid'];
				$url='https://m.58daiyan.com/user/order_info/id/'.$order_id;
				$keyword1=$order_info['order_sn'];
				$keyword2=get_wuliu_info($order_info['shipping_company_id']);
				$keyword3=$order_info['shipping_code'];
				send_wuliu($openid,$url,$keyword1,$keyword2,$keyword3,$keyword4);
			}
		}else{
			$message['code']=0;
			$message['message']='发货失败';
		}
		$this->ajaxReturn($message);
  	}
  }
  
  function order_return(){
  	$order=M('order');
  	$users=M('users');
  	$order_id=I('get.order_id');
  	$order_info=$order->find($order_id);
  	$status=I('get.status');
  	$data['order_id']=$order_id;
  	$data['refund_agreen_time']=time();
  	$storeArray  =   $this->storeArray;
  	$store_id=$storeArray['id'];
  	if($status==3){
  		$data['refund_state']=3;
  		$log['refund_state']=3;	
  		$log['order_status_text']='总后台拒绝';
  	}
  	if($status==2){
  		if($order_info['refund_type']==1){
  			$data['refund_state']=4;
  			$log['refund_state']=4;	
  		}else{
  			$data['refund_state']=2;
  			$log['refund_state']=2;
  		}
  		$log['order_status_text']='总后台同意';
  		
  	}
  	if($status==4){
  		$data['refund_state']=4;
  		$log['refund_state']=4;
  		$log['order_status_text']='总后台同意';
  	}
  	$rs=$order->save($data);
  	if($rs){
  		$search['refund_state']=$log['refund_state'];
  		
  		$log['order_id']=$order_id;
		$log['action']='处理退换货订单';
		$log['user_name']='总后台';

		$log['action_time']=time();
		M('order_action')->add($log);
	}
	$this->redirect('/Mall/orderdetail/id/'.$order_id);
  }
  
  function edit_price(){
  	$userArray	=	$this->userArray;
  	$uid=$userArray['id'];
  	$price=I('post.cprice');
  	$order_id=I('post.order_id');
  	$order_info=M('order')->find($order_id);
  	
  	
  	$log['order_id']=$order_id;
  	$log['action']='修改订单价格：原价-￥'.$order_info['order_amount'].',修改后￥'.$price;
  	$log['uid']=$uid;
  	$log['user_name']='总后台';
  	$log['action_time']=time();
  	$log['yprice']=$order_info['order_amount'];
  	$log['xprice']=$price;
  	
  	$data['order_id']=$order_id;
  	$data['order_amount']=$price;
  	$rs=M('order')->save($data);
  	if($rs){
  		$log_info=M('order_price_action')->add($log);
  		if($log_info){
  			$this->ajaxReturn(1);
  		}else{
  			$this->ajaxReturn(0);
  		}
  	}else{
  		$this->ajaxReturn(0);
  	}
  	
  }

}
